/*
 * @Author: your name
 * @Date: 2024-03-21 22:45:58
 * @LastEditTime: 2024-03-22 09:57:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\844.比较含退格的字符串.cpp
 */
/*
 * @lc app=leetcode.cn id=844 lang=cpp
 *
 * [844] 比较含退格的字符串
 */

// @lc code=start
class Solution {
public:
    bool backspaceCompare(string s, string t) {
        int slow = 0;
        for (int fast = 0; fast < s.size(); fast++)
        {
            if((char)s[fast] != '#'){
                s[slow++] = s[fast];
            }
            else{
                if(slow > 0)
                    slow--;
            }
        }
        string ss = s.substr(0,slow);
        slow = 0;
        for (int fast = 0; fast < t.size(); fast++)
        {
            if((char)t[fast] != '#'){
                t[slow++] = t[fast];
            }
            else{
                if(slow > 0)
                    slow--;
            }
        }
        string tt = t.substr(0,slow);
        
        return !tt.compare(ss);
    }
};
// @lc code=end

// 分别对两个string做#的过滤，要注意若是遇到#,需要使slow--，实现退格操作